package com;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.table.DefaultTableModel;

public class MainFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	private static MainFrame mf;
	private ObjectOutputStream out;
	private JTextArea usertextarea=new JTextArea();
	private JTable usertable=new JTable();
	private DefaultTableModel dtm;
	private JTextField chattf;
	private JButton sendbt;
	private JTextArea chatArea;
	private ChessPanel chesspanel;
	private LoginPanel loginPanel;
	private Socket socket;
	Integer port;
	boolean isLinked;
	User user;
	User opponent;
	private Map<Integer, Integer> map;
	
	
	public void setSocket(Socket socket){
			this.socket=socket;
	}
	
	public Socket getSocket(){
		return socket;
	}
	
	public ChessPanel getChessPanel(){
		return chesspanel;
	}
	
	public void send(Socket socket,Object message){
		try {
			out=new ObjectOutputStream(socket.getOutputStream());
			out.writeObject(message);
			out.flush();
		} catch (IOException e) {
			System.out.println("发送失败！");
		}
	}
	
	public Integer getPort(){
		return map.get(port);
	}
	
	public void setUser(User user){
		if (!user.isOpponent()) {
			usertextarea.setText("昵称：" + user.getName() + "\n");
			usertextarea.append("IP：" + user.getHost().getHostAddress()+ "\n");
			this.user=user;
			chesspanel.leftInfoLabel.setText(user.getName());
		}
		else{
			  this.opponent = user;
			  chesspanel.rightInfoLabel.setText(user.getName());
			}
		Vector<Object> row = new Vector<Object>();
		row.add(user.getName());
		row.add(user.getHost().getHostName());
		row.add(user.getTime());
		dtm.addRow(row);
	}
	
	public void getChessColor(){
		if(user.getTime().getTime() > opponent.getTime().getTime()) 
		{
			getChessPanel().getGridPanel().setChessColor(Color.white);
			getChessPanel().getGridPanel().setMy(true);
		}
		else
		{
			getChessPanel().getGridPanel().setChessColor(Color.black);
			getChessPanel().getGridPanel().setMy(false);
		}
	}
	
	public MainFrame() {
		isLinked=false;
		map=new HashMap<Integer, Integer>();
		map.put(9988, 9989);
		map.put(9989, 9988);
		initCompnents();
		ServerThread rt=new ServerThread(this);
		Thread th=new Thread(rt);
		th.start();
		ClientThreed ct=new ClientThreed(this);
		Thread th1=new Thread(ct);
		th1.start();
		loginPanel=new LoginPanel();
		setGlassPane(loginPanel);
		loginPanel.setVisible(true);
		mf=this;
	}
	
	
	private void initCompnents() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("五子棋--网络版");
		setVisible(true);
		chesspanel=new ChessPanel();
		add(chesspanel,BorderLayout.CENTER);
		//
		JPanel panel1=new JPanel();
		panel1.setLayout(new BoxLayout(panel1, BoxLayout.PAGE_AXIS));
		JPanel panel3=new JPanel();
		panel3.setLayout(new BorderLayout());
		panel3.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
		panel3.setPreferredSize(new Dimension(225, 50));
		JLabel label1=new JLabel();
		label1.setIcon(new ImageIcon(getClass().getResource("/res/infoPanelLeft.png")));
		panel3.add(label1, BorderLayout.WEST);
		usertextarea.setColumns(20);
		usertextarea.setEditable(false);
		usertextarea.setLineWrap(true);
		usertextarea.setRows(5);
		JScrollPane sp1=new JScrollPane();
		panel3.add(sp1, BorderLayout.CENTER);
		sp1.setViewportView(usertextarea);
		panel1.add(panel3);
		//
		JPanel panel4=new JPanel();
		panel4.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
		panel4.setPreferredSize(new Dimension(100, 20));
		panel4.setLayout(new BorderLayout());
		JScrollPane sp2=new JScrollPane();
		sp2.setMaximumSize(new Dimension(32767, 30));
		sp2.setPreferredSize(new Dimension(241, 30));
		dtm=new DefaultTableModel(new Object[][]{}, new String[]{"昵称", "主机", "联机时间"});
		usertable.setModel(dtm);
		usertable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		sp2.setViewportView(usertable);
		panel4.add(sp2,BorderLayout.CENTER);
		panel1.add(panel4);
		//
		JPanel panel2=new JPanel();
		panel2.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
		panel2.setPreferredSize(new Dimension(100, 300));
		panel2.setLayout(new BorderLayout());
		JPanel panel5=new JPanel();
		panel5.setLayout(new BoxLayout(panel5,BoxLayout.LINE_AXIS));
		chattf=new JTextField();
		panel5.add(chattf);
		sendbt=new JButton("发送");
		sendbt.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				String s=user.getName()+"说："+chattf.getText()+"\n";
				send(socket, s);
				chattf.setText("");
			}
		});
		panel5.add(sendbt);
		panel2.add(panel5,BorderLayout.SOUTH);
		chatArea=new JTextArea();
		chatArea.setColumns(20);
		chatArea.setEditable(false);
		chatArea.setLineWrap(true);
		chatArea.setTabSize(4);
		chatArea.setFont(new Font("宋体", Font.TRUETYPE_FONT, 14));
		JScrollPane sp3=new JScrollPane(chatArea);
		panel2.add(sp3,BorderLayout.CENTER);
		panel1.add(panel2);
		add(panel1,BorderLayout.EAST);
		Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((screensize.width-1000)/2, (screensize.height-700)/2, 1000, 700);
	}
	
	
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {			
			@Override
			public void run() {
				try {
					UIManager.setLookAndFeel(new NimbusLookAndFeel());
					new MainFrame();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		});
	}
	public static MainFrame getInstence(){
		return mf;
	}
	
	public void appendMessage(String message){
		Runnable runnable=new Runnable() {			
			@Override
			public void run() {
				chatArea.append(message);
			}
		};
		if(SwingUtilities.isEventDispatchThread()){
			runnable.run();
		}
		else{
			SwingUtilities.invokeLater(runnable);
		}
	}

}
